package com.sy.module_clockin.db

import androidx.annotation.NonNull
import androidx.room.TypeConverter
import java.util.*
import com.google.gson.reflect.TypeToken

import com.google.gson.Gson


/**
 * 类型转换器
 */
class Converters {
    @TypeConverter
    fun fromTimestamp(@NonNull value: Long): Date {
        return Date(value)
    }

    @TypeConverter
    fun dateToTimestamp(@NonNull date: Date): Long {
        return date.time
    }

    @TypeConverter
    fun fromString(value: String): Pair<Int, Int> {
        val split = value.replace("(", "").replace(")", "").split(", ")
        return Pair(split[0].toInt(), split[1].toInt())
    }

    @TypeConverter
    fun pairToString(pair: Pair<Int, Int>): String {
        return pair.toString()
    }

    @TypeConverter
    fun fromList(datas: List<String>): String {
        return Gson().toJson(datas)
    }

    @TypeConverter
    fun toList(str: String): MutableList<String> {
        return Gson().fromJson(str, TypeToken.getParameterized(MutableList::class.java, String::class.java).type)

    }
}